package com.pengweiwei.androidzygote.functions.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.pengweiwei.androidzygote.BR;
import com.pengweiwei.androidzygote.R;
import com.putong35.common.baseactfrag.adapter.DataBindRecyclerViewAdapter;
import com.putong35.common.baseactfrag.adapter.RecyclerViewBaseAdapter;
import com.putong35.common.baseactfrag.fragment.SwipeListFragment;
import com.putong35.common.httprequest.Response;
import com.putong35.common.utils.io.AssetsUtil;

import java.util.List;


/**
 * 刷列表的模板
 * <p>
 * 主要分为三步：fragment  + adapter(+ layout布局)  + bean
 * <p>
 * 2. adapter要设置 泛型参数（LoginResponce）  e.g extends RecyclerViewBaseAdapter<LoginResponce>
 */
public class DataBindListFragment extends SwipeListFragment {

//    private final static String URL_TEST = "http://hpapi.baoruan.com:10081/test/list";
private final static String URL_TEST = "http://apitest.baoruan.com/launcher/default/index?version=9.150320&un=239BA6301606E596F70AC1E7B4808B23&name=recommendlist&channelId=ceshi_baoruanservice1";

    private int mPage = 0;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        init();
        return view;
    }

    private void init() {
        mPage = 0;
        clearDecorations();
        setPullToRefreshEnable(false);
    }

    @Override
    public void onResume() {
        super.onResume();
        startLoading();
    }

//    private ListDataRequestListener<TemplateBean> mResponceListener = new ListDataRequestListener<>(this, TemplateBean.class);

    @Override
    public RecyclerViewBaseAdapter createAdapter() {
        return new DataBindRecyclerViewAdapter<DataBindListActivity.TemplateBean>(getActivity(), R.layout.listitem_template_data_bind, BR.itemBean);
    }


    @Override
    public void onStartLoading() {
        String responseStr = AssetsUtil.getStringFromAssets(getActivity(), "list_ui_test_data.txt");
        Response parser = new Response(responseStr);
        List<DataBindListActivity.TemplateBean> list = parser.getDataListBean(DataBindListActivity.TemplateBean.class);
        if (list != null && list.size() > 0) {
            getAdapter().getData().addAll(list);
        }

    }




}
